﻿using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SimpleClient = Simple.PrettyClient.IClient;

/*********************************************************
 * 命名空间 Simple.WindowsServer
 * 接口名称 HostExtension
 * 开发人员：11920
 * 创建时间：2023/4/24 9:22:38
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.ClientWindowsServer
{
	public static class HostExtension
	{
		private static IHostLifetime hostLifeTime;

		public static SimpleClient UseWindowsService(this SimpleClient client)
		{
			try
			{
#if DEBUG

#else
				Regist(client);
				CancellationToken cancellationToken = new CancellationToken();
				hostLifeTime.WaitForStartAsync(cancellationToken);
#endif
			}
			catch (Exception ex)
			{

			}
			return client;
		}


		private static void Regist(SimpleClient client)
		{
			client.Regster((container) =>
			{
				container.RegisterSingleton<Microsoft.Extensions.Hosting.IHostEnvironment, HostingEnvironment>();
				container.RegisterSingleton<IHostApplicationLifetime, HostApplicationLifetime>();
				container.RegisterSingleton<IHostLifetime, SimpleHostLifeTime>();
				hostLifeTime = container.GetService<IHostLifetime>();
			});
		}
	}
}
